package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.entity.coinStore.AttrsSelect;
import com.zhongxi.guoyuan.entity.coinStore.SelcetPostCartGoodsAttrs;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.DataUtil;

import java.math.BigDecimal;
import java.util.List;

import static com.zhongxi.guoyuan.R.id.tv_reward_coin;

/**
 * Created by Administrator on 2017/8/25.
 */

public class RewardItemOrderAdapter extends BaseAdapter {

    private static final int ITEM_LAST = 0;
    private static final int ITEM_DEFAULT = 1;
    private AttrsSelect attrs;
    private String postage;
    private String totalPrice;
    private List<SelcetPostCartGoodsAttrs> goodsAttrses;
    private Context context;

    public RewardItemOrderAdapter(Context context, List<SelcetPostCartGoodsAttrs> goodsAttrses, AttrsSelect attrs, String totalPrice, String postage) {
        this.context = context;
        this.goodsAttrses = goodsAttrses;
        this.attrs = attrs;
        this.totalPrice = totalPrice;
        this.postage = postage;
    }

    @Override
    public int getCount() {
        if (goodsAttrses != null && goodsAttrses.size() > 0) {
            return goodsAttrses.size() + 1;
        } else if (attrs != null && !"".equals(attrs)) {
            return 2;
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return goodsAttrses.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (goodsAttrses != null && goodsAttrses.size() > 0) {
            if (position == goodsAttrses.size()) {
                return ITEM_LAST;
            }
        } else {
            if (position == 1) {
                return ITEM_LAST;
            }
        }
        return ITEM_DEFAULT;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if (view == null) {
            vh = new ViewHolder();
            switch (getItemViewType(i)) {
                case ITEM_LAST:
                    view = View.inflate(context, R.layout.item_reward_pay_last, null);
                    vh.tv_total_price = (TextView) view.findViewById(R.id.tv_total_price);
                    vh.tv_postage = (TextView) view.findViewById(R.id.tv_postage);
                    break;
                case ITEM_DEFAULT:
                    view = View.inflate(context, R.layout.item_market_pay, null);
                    vh.ivImg = (ImageView) view.findViewById(R.id.ivImg);
                    vh.tvName = (TextView) view.findViewById(R.id.tvName);
                    vh.tvNum = (TextView) view.findViewById(R.id.tvNum);
                    vh.tvAttr = (TextView) view.findViewById(R.id.tvAttr);
                    vh.tvCash = (TextView) view.findViewById(R.id.tvCash);
                    vh.tv_reward_yield = (TextView) view.findViewById(R.id.tv_reward_yield);
                    vh.tv_reward_coin = (TextView) view.findViewById(tv_reward_coin);
                    break;
            }
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }

        if (goodsAttrses != null && goodsAttrses.size() > 0) {
            if (i == goodsAttrses.size()) {
                vh.tv_total_price.setText("合计: " + totalPrice + " 元");
                vh.tv_postage.setText("运费: " + postage + " 元");
            } else {
                SelcetPostCartGoodsAttrs goodsAttrs = goodsAttrses.get(i);
                Glide.with(context).load(Url.IMG_URL + goodsAttrs.getImgUrl()).placeholder(R.mipmap.iv_eg).fitCenter().into(vh.ivImg);
                if (goodsAttrs.getName() != null && !"".equals(goodsAttrs.getName())) {
                    vh.tvName.setText(goodsAttrs.getName());
                }
                if (goodsAttrs.getGoodsnum() != null && !"".equals(goodsAttrs.getGoodsnum())) {
                    vh.tvNum.setText("x" + goodsAttrs.getGoodsnum());
                }
                if (goodsAttrs.getPrice() != null && !"".equals(goodsAttrs.getPrice())) {
                    vh.tvCash.setText(goodsAttrs.getPrice() + " 元");
                } else {
                    vh.tvCash.setText("0 元");
                }

                if (goodsAttrs.getItemGoodAttrs() != null && goodsAttrs.getItemGoodAttrs().size() > 0) {
                    String str = "";
                    for (int j = 0; j < goodsAttrs.getItemGoodAttrs().size(); j++) {
                        str += goodsAttrs.getItemGoodAttrs().get(j).getAttrv() + "/";
                    }
                    vh.tvAttr.setText("规格:" + str.substring(0, str.length() - 1));
                }
            }
        } else {
            if (i == 0) {
                if (attrs.getItemMaps() != null && attrs.getItemMaps().size() > 0) {
                    String str = "";
                    for (String attr : attrs.getItemMaps().keySet()) {
                        str += attr + "/";
                    }
                    vh.tvAttr.setText("规格:" + str.substring(0, str.length() - 1));
                }
                if (attrs.getName() != null && !"".equals(attrs.getName())) {
                    vh.tvName.setText(attrs.getName());
                } else {
                    vh.tvName.setText("");
                }
                Glide.with(context).load(Url.IMG_URL + attrs.getThumbnail()).placeholder(R.mipmap.iv_eg).fitCenter().into(vh.ivImg);
                if (attrs.getGoodsnum() != null && !"".equals(attrs.getGoodsnum())) {
                    vh.tvNum.setText("x" + attrs.getGoodsnum());
                }
                if (attrs.getPrice() != null && !"".equals(attrs.getPrice())) {
                    vh.tvCash.setText(attrs.getPrice() + " 元");
                }
            } else {
                vh.tv_total_price.setText("合计: " + totalPrice + " 元");
                vh.tv_postage.setText("运费: " + postage + " 元");
            }
        }
        return view;
    }

    class ViewHolder {
        private TextView tv_total_price;
        private TextView tv_postage;

        private ImageView ivImg;
        private TextView tvName;
        private TextView tvNum;
        private TextView tvAttr;
        private TextView tvCash;
        private TextView tv_reward_yield;
        private TextView tv_reward_coin;
    }
}
